package com.ruoyi.quartz.config;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.binarywang.wxpay.bean.result.WxPayRedpackQueryResult;
import com.github.binarywang.wxpay.bean.result.WxPaySendRedpackResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.im.service.IImRoomUserService;
import com.ruoyi.pay.domain.PayCoinCash;
import com.ruoyi.pay.domain.PayCoinUser;
import com.ruoyi.pay.domain.PayCoinUserForm;
import com.ruoyi.pay.service.IPayCoinCashService;
import com.ruoyi.pay.service.IPayCoinUserService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqDynamic;
import com.ruoyi.wq.domain.WqNoticeChat;
import com.ruoyi.wq.domain.WqVote;
import com.ruoyi.wq.service.IWqDynamicService;
import com.ruoyi.wq.service.IWqFriendApplyService;
import com.ruoyi.wq.service.IWqNoticeChatService;
import com.ruoyi.wq.service.IWqVoteService;
import com.ruoyi.wx.domain.WxMaForm;
import com.ruoyi.wx.domain.WxMaMsgForm;
import com.ruoyi.wx.domain.WxMpMsgForm;
import com.ruoyi.wx.service.WxMaPayService;
import com.ruoyi.wx.service.WxMpUserService;
import com.ruoyi.wx.service.WxMsgService;
import cn.hutool.core.date.DateUtil;

@Component
public class CorntabConfig {
	
	@Autowired
	private ISysUserService sysUserService;
	
	@Autowired
	private IWqDynamicService wqDynamicService;
	
	@Autowired
	private IWqFriendApplyService wqFriendApplyService;
	
	@Autowired
	private IWqNoticeChatService wqNoticeChatService;
	
	@Autowired
	private IPayCoinCashService payCoinCashService;
	
	@Autowired
	private IImRoomUserService imRoomUserService;
	
	@Autowired
	private IPayCoinUserService payCoinUserService;
	
	@Autowired
	private IWqVoteService wqVoteService;
	
	@Autowired
    private WxMsgService wxMsgService;
	
	@Autowired
	private RedisCache redisCache;
	
	@Autowired
	private WxMaPayService wxMaPayService;
	
	@Autowired
	private WxMpUserService wxMpUserService;
	
	private Integer start = 0;
	
	private Integer end = 500;

	public final static String KEY_SCORE = "key:score";
    
	public final static String KEY_USER = "key:user";
	
	//@Scheduled(cron = "0 */30 * * * ?")
	public void cronTest(){
		//sysUserService.updateUserPlan();
    	wqDynamicService.updateDynamicPlan();
	}
	
	//@Scheduled(cron = "*/5 * * * * ?")
	public void cronTest88(){
		Map<String,Object> map = redisCache.getCacheMap("voteMapScore");
		for (String key : map.keySet()) {
			Integer value = (Integer) map.get(key);
			if(value > 0){
				Long rank = redisCache.zrevrank("voteScore", key);
				Double score = redisCache.zscore("voteScore", key);
				
				// 同步到数据库
				WqVote vote = wqVoteService.getById(key);
				Long voteNum = new Double(score).longValue();
				vote.setVoteNum(voteNum);
				vote.setCityRank(rank+1);
				wqVoteService.updateById(vote);
				redisCache.delCacheMapValue("voteMapScore", key);
			}
		}
	}
	
	//@Scheduled(cron = "0 */50 * * * ?")
	public void cronTest2(){
		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
		SysUser entity = new SysUser();
		entity.setWxId("gh_b0cfae9edc73");
		entity.setIsWelcome(1);
		entity.setPayNum(0);
		queryWrapper.setEntity(entity).isNotNull("wx_open_id");
		List<SysUser> userList = sysUserService.list(queryWrapper);
		for(SysUser user: userList){
			user.setLoginDate(DateUtil.date());
			sysUserService.updateById(user);
		}
	}
	
	@Scheduled(cron = "0 0 0 * * ?")
	public void clearRank() {
		System.out.println("删除排行榜前100名");
		redisCache.deleteObject(KEY_SCORE);
		redisCache.deleteObject(KEY_USER);
	}
	
	//@Scheduled(cron = "0 13 22 * * ?")
	public void clearRank2() {
		QueryWrapper<SysUser> qw = new QueryWrapper<>();
		SysUser en = new SysUser();
		en.setAppId("wx4a8ce56c55215ee0");
		en.setStatus("0");
		en.setIsSend("0");
		qw.setEntity(en);
		qw.gt("tip_num", 0);
		qw.orderByAsc("create_time");
		List<SysUser> ul = sysUserService.list(qw);
		for(SysUser u : ul){
			WxMaMsgForm msgForm = new WxMaMsgForm();
			msgForm.setAppId(u.getAppId());
			msgForm.setOpenId(u.getOpenId());
			Map<String,String> dataMap = new HashMap<>();
			dataMap.put("phrase1", "抢会员活动");
			dataMap.put("date2", DateUtil.now());
			dataMap.put("name3", "系统通知");
			dataMap.put("thing4", "限单身参加，前五十名赠送体验会员");
			msgForm.setDataMap(dataMap);
			msgForm.setPage("pages/index/rank");
			wxMsgService.sendSubscribeMsg(msgForm);
			
			u.setIsSend("1");
			u.setTipNum(0);
			sysUserService.updateById(u);
		}
	}
	
	//@Scheduled(cron = "0 12 2 * * ?")
	public void cronTest3(){
		// 给没有发送过的发送
		QueryWrapper<SysUser> qw = new QueryWrapper<>();
		SysUser en = new SysUser();
		en.setAppId("wx2b5596fc2c4454d4");
		en.setStatus("0");
		en.setIsAuth("1");
		en.setIsTip("0");
		qw.setEntity(en);
		List<SysUser> ul = sysUserService.list(qw);
		Map<String,String> map = new HashMap<>();
		for(SysUser u : ul){
			WxMaMsgForm msgForm = new WxMaMsgForm();
			msgForm.setAppId(u.getAppId());
			msgForm.setOpenId(u.getOpenId());
			Map<String,String> dataMap = new HashMap<>();
			dataMap.put("phrase1", "活动邀请");
			dataMap.put("date2", DateUtil.now());
			dataMap.put("name3", "cc");
			dataMap.put("thing4", "赠送200趣豆请查收");
			msgForm.setDataMap(dataMap);
			msgForm.setPage("pages/game/share?my=true");
			boolean flag = wxMsgService.sendSubscribeMsg(msgForm);
			
			if(flag){
				PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
		    	payCoinUserForm.setCoinNum(200);
		    	payCoinUserForm.setCoinType("0");
		    	payCoinUserForm.setRemark("活动奖励");
		    	payCoinUserForm.setUserId(u.getUserId());
		    	
		    	PayCoinUser coinUser = new PayCoinUser();
				coinUser.setUserId(payCoinUserForm.getUserId());
				coinUser.setCoinNum(payCoinUserForm.getCoinNum());
				coinUser.setCoinType(payCoinUserForm.getCoinType());
				coinUser.setRemark(payCoinUserForm.getRemark());
				payCoinUserService.save(coinUser);
				
				map.put(u.getOpenId(), u.getOpenId());
			}
			u.setIsTip("1");
			sysUserService.updateById(u);
		}
		
		// 给已经订阅的发送
		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
		SysUser entity = new SysUser();
		entity.setAppId("wx2b5596fc2c4454d4");
		entity.setStatus("0");
		queryWrapper.setEntity(entity);
		queryWrapper.gt("tip_num", 0);
		List<SysUser> userList = sysUserService.list(queryWrapper);
		for(SysUser user : userList){
			if(map.containsKey(user.getOpenId())){
				continue;
			}
			WxMaMsgForm msgForm = new WxMaMsgForm();
			msgForm.setAppId(user.getAppId());
			msgForm.setOpenId(user.getOpenId());
			Map<String,String> dataMap = new HashMap<>();
			dataMap.put("phrase1", "活动邀请");
			dataMap.put("date2", DateUtil.now());
			dataMap.put("name3", "cc");
			dataMap.put("thing4", "赠送200趣豆请查收");
			msgForm.setDataMap(dataMap);
			msgForm.setPage("pages/game/share?my=true");
			boolean flag = wxMsgService.sendSubscribeMsg(msgForm);
			
			if(flag){
				PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
		    	payCoinUserForm.setCoinNum(200);
		    	payCoinUserForm.setCoinType("0");
		    	payCoinUserForm.setRemark("活动奖励");
		    	payCoinUserForm.setUserId(user.getUserId());
		    	
		    	PayCoinUser coinUser = new PayCoinUser();
				coinUser.setUserId(payCoinUserForm.getUserId());
				coinUser.setCoinNum(payCoinUserForm.getCoinNum());
				coinUser.setCoinType(payCoinUserForm.getCoinType());
				coinUser.setRemark(payCoinUserForm.getRemark());
				payCoinUserService.save(coinUser);
			}
			
			user.setTipNum(0);
			sysUserService.updateById(user);
		}
	}
	
	//@Scheduled(cron = "0 46 21 * * ?")
	public void dd() {
		WxMaForm form = new WxMaForm();
		form.setAppId("wxb3279108e13e4e57");
		form.setOpenId("oKKJe6aEo8nNQvE8oBewd12Xiuo0");
		form.setTradeNo("132");
		form.setTotalFee(10000);
		WxPaySendRedpackResult result;
		try {
			result = wxMaPayService.sendRedpack(form, "提现");
			System.out.println("发送红包");
			System.out.println(result.getReturnMsg());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	// 让用户注册
	//@Scheduled(cron = "*/3 * * * * ?")
	public void cronTestt(){
		System.out.println("执行");
		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
		SysUser entity = new SysUser();
		entity.setStatus("0");
		entity.setIsAuth("0");
		entity.setIsSend("0");
		entity.setIsWelcome(1);
		queryWrapper.setEntity(entity);
		queryWrapper.orderByAsc("user_id");
		//queryWrapper.notIn("app_id", "wx4a8ce56c55215ee0","wx4e2d1d8ce0d8f454");
		List<SysUser> userList = sysUserService.list(queryWrapper);
		for(SysUser user : userList){
			WxMaMsgForm msgForm = new WxMaMsgForm();
			msgForm.setAppId(user.getAppId());
			msgForm.setOpenId(user.getOpenId());
			Map<String,String> dataMap = new HashMap<>();
			dataMap.put("phrase1", "交友邀请");
			dataMap.put("date2", DateUtil.now());
			dataMap.put("name3", "cc");
			dataMap.put("thing4", "您收到一封情书请查收");
			msgForm.setDataMap(dataMap);
			msgForm.setPage("packgeMine/pages/me/welcome/welcome");
			boolean flag = wxMsgService.sendSubscribeMsg(msgForm);
			if(flag){
				user.setIsAuth("1");
				user.setIsAgent("1");
				user.setIsWelcome(0);
				user.setIsSend("1");
				sysUserService.updateById(user);
			}else{
				user.setIsSend("1");
				sysUserService.updateById(user);
			}
		}
	}
	
	// 唤醒会员
	//@Scheduled(cron = "0 10 16 * * ?")
	public void cronTestt2(){
		System.out.println("执行");
		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
		SysUser entity = new SysUser();
		entity.setStatus("0");
		entity.setIsSend("0");
		queryWrapper.setEntity(entity);
		queryWrapper.gt("pay_num", 100);
		queryWrapper.orderByAsc("login_date");
		List<SysUser> userList = sysUserService.list(queryWrapper);
		for(SysUser user : userList){
			WxMaMsgForm msgForm = new WxMaMsgForm();
			msgForm.setAppId(user.getAppId());
			msgForm.setOpenId(user.getOpenId());
			Map<String,String> dataMap = new HashMap<>();
			dataMap.put("phrase1", "活动奖励");
			dataMap.put("date2", DateUtil.now());
			dataMap.put("name3", "cc");
			dataMap.put("thing4", "赠送200趣豆请查收");
			msgForm.setDataMap(dataMap);
			msgForm.setPage("pages/game/share?my=true");
			boolean flag = wxMsgService.sendSubscribeMsg(msgForm);
			if(flag){
				user.setIsAuth("1");
				user.setIsAgent("1");
				user.setIsTip("1"); // 通知成功
				user.setIsSend("1"); // 通知过
				user.setTipNum(0); // 登陆标记
				sysUserService.updateById(user);
				
				PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
		    	payCoinUserForm.setCoinNum(200);
		    	payCoinUserForm.setCoinType("0");
		    	payCoinUserForm.setRemark("活动奖励");
		    	payCoinUserForm.setUserId(user.getUserId());
		    	
		    	PayCoinUser coinUser = new PayCoinUser();
				coinUser.setUserId(payCoinUserForm.getUserId());
				coinUser.setCoinNum(payCoinUserForm.getCoinNum());
				coinUser.setCoinType(payCoinUserForm.getCoinType());
				coinUser.setRemark(payCoinUserForm.getRemark());
				payCoinUserService.save(coinUser);
			}else{
				// 小程序没通知成功，继续用公众号通知
				WxMpMsgForm mpForm = new WxMpMsgForm();
				mpForm.setWxId(user.getWxId());
				mpForm.setAppId(user.getAppId());
				mpForm.setOpenId(user.getWxOpenId());
				Map<String,String> mpMap = new HashMap<>();
				mpMap.put("first", "活动奖励");
				mpMap.put("keyword1", "cc");
				mpMap.put("keyword2", DateUtil.now());
				mpMap.put("keyword3", "赠送200趣豆请查收");
				mpMap.put("remark", "本条信息是站内信通知，祝您早日脱单！点击查看详情");
				mpForm.setDataMap(mpMap);
				mpForm.setPage("pages/game/share?my=true");
				boolean mpFlag = wxMsgService.sendTemplateMsg(mpForm);
				if(mpFlag){
					user.setIsAuth("1");
					user.setIsAgent("1");
					user.setIsTip("1"); // 通知成功
					user.setIsSend("1"); // 通知过
					user.setTipNum(0); // 登陆标记
					sysUserService.updateById(user);
					
					PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
			    	payCoinUserForm.setCoinNum(200);
			    	payCoinUserForm.setCoinType("0");
			    	payCoinUserForm.setRemark("活动奖励");
			    	payCoinUserForm.setUserId(user.getUserId());
			    	
			    	PayCoinUser coinUser = new PayCoinUser();
					coinUser.setUserId(payCoinUserForm.getUserId());
					coinUser.setCoinNum(payCoinUserForm.getCoinNum());
					coinUser.setCoinType(payCoinUserForm.getCoinType());
					coinUser.setRemark(payCoinUserForm.getRemark());
					payCoinUserService.save(coinUser);
				}else{
					user.setIsSend("1"); // 通知过
					user.setIsTip("0"); // 通知失败
					user.setTipNum(0); // 登陆标记
					sysUserService.updateById(user);
				}
			}
		}
	}
	
	//@Scheduled(cron = "0 58 22 * * ?")
	public void cronTestt2d(){
		System.out.println("执行");
		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
		SysUser entity = new SysUser();
		entity.setStatus("0");
		entity.setIsSend("0");
		entity.setAppId("wxb3279108e13e4e57");
		queryWrapper.setEntity(entity);
		queryWrapper.orderByAsc("create_time");
		List<SysUser> userList = sysUserService.list(queryWrapper);
		for(int i=0;i<userList.size();i++){
			SysUser user = userList.get(i);
			WxMaMsgForm msgForm = new WxMaMsgForm();
			msgForm.setAppId(user.getAppId());
			msgForm.setOpenId(user.getOpenId());
			Map<String,String> dataMap = new HashMap<>();
			dataMap.put("phrase1", "认证通知");
			dataMap.put("date2", DateUtil.now());
			dataMap.put("name3", "夜暮");
			dataMap.put("thing4", "我想认识你，邀请你完善微信");
			msgForm.setDataMap(dataMap);
			msgForm.setPage("pages/index?index=1");
			boolean flag = wxMsgService.sendSubscribeMsg(msgForm);
			if(flag){
				user.setIsTip("1"); // 通知成功
				sysUserService.updateById(user);
				
				WqNoticeChat noticeChat = new WqNoticeChat();
				noticeChat.setFromUserId(11640806L);
				noticeChat.setToUserId(user.getUserId());
				
				noticeChat.setNoticeType("WECHAT");
				noticeChat.setRemark("我想认识你，邀请你完成微信认证，请在个人资料完善微信");
				wqNoticeChatService.saveOrUpdate(noticeChat);
			}else{
				// 小程序没通知成功，继续用公众号通知
				WxMpMsgForm mpForm = new WxMpMsgForm();
				mpForm.setWxId(user.getWxId());
				mpForm.setAppId(user.getAppId());
				mpForm.setOpenId(user.getWxOpenId());
				Map<String,String> mpMap = new HashMap<>();
				mpMap.put("first", "邀请认证通知");
				mpMap.put("keyword1", "夜暮");
				mpMap.put("keyword2", DateUtil.now());
				mpMap.put("keyword3", "我想认识你，邀请你完成微信认证，请在个人资料完善微信");
				mpMap.put("remark", "本条信息是站内信通知，祝您早日脱单！点击查看详情");
				mpForm.setDataMap(mpMap);
				mpForm.setPage("pages/index?index=1");
				boolean mpFlag = wxMsgService.sendTemplateMsg(mpForm);
				if(mpFlag){
					user.setIsTip("1"); // 通知成功
					sysUserService.updateById(user);
					
					WqNoticeChat noticeChat = new WqNoticeChat();
					noticeChat.setFromUserId(11640806L);
					noticeChat.setToUserId(user.getUserId());
					
					noticeChat.setNoticeType("WECHAT");
					noticeChat.setRemark("我想认识你，邀请你完成微信认证，请在个人资料完善微信");
					wqNoticeChatService.saveOrUpdate(noticeChat);
				}
			}
			
			user.setIsSend("1"); // 通知过
			sysUserService.updateById(user);
		}
	}
	
	// 唤醒最近7天用户
	@Scheduled(cron = "0 30 18 * * ?")
	public void cronTestt22(){
		System.out.println("执行");
		Date date = DateUtil.offsetDay(DateUtil.date(), -7);
		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("status", "0");
		queryWrapper.in("app_id", "wx4a8ce56c55215ee0","wx4e2d1d8ce0d8f454","wx2708b75e03e8440a");
		queryWrapper.gt("login_date", date);
		queryWrapper.orderByAsc("create_time");
		List<SysUser> userList = sysUserService.list(queryWrapper);
		for(SysUser user : userList){
			if(user.getLoginDate() != null){
				// 超过一天没上线发送通知
				WxMaMsgForm msgForm = new WxMaMsgForm();
				msgForm.setAppId(user.getAppId());
				msgForm.setOpenId(user.getOpenId());
				Map<String,String> dataMap = new HashMap<>();
				dataMap.put("phrase1", "活动通知");
				dataMap.put("date2", DateUtil.now());
				dataMap.put("name3", "心动匹配");
				dataMap.put("thing4", "心动匹配开始了，快去加入Ta们吧");
				msgForm.setDataMap(dataMap);
				msgForm.setPage("pages/index?isAuth=1&index=2");
				boolean flag = wxMsgService.sendSubscribeMsg(msgForm);
				if(!flag){
					// 小程序没通知成功，继续用公众号通知
					WxMpMsgForm mpForm = new WxMpMsgForm();
					mpForm.setWxId(user.getWxId());
					mpForm.setAppId(user.getAppId());
					mpForm.setOpenId(user.getWxOpenId());
					Map<String,String> mpMap = new HashMap<>();
					mpMap.put("first", "活动通知");
					mpMap.put("keyword1", "心动匹配");
					mpMap.put("keyword2", DateUtil.now());
					mpMap.put("keyword3", "心动匹配开始了，快去加入Ta们吧");
					mpMap.put("remark", "本条信息是站内信通知，祝您早日脱单！点击查看详情");
					mpForm.setDataMap(mpMap);
					mpForm.setPage("pages/index?isAuth=1&index=2");
					wxMsgService.sendTemplateMsg(mpForm);
				}
			}
		}
	}
	
	
	// 唤醒用户活动
	//@Scheduled(cron = "0 0 20 * * ?")
	public void cronTestt222(){
		System.out.println("执行");
		// 初始化数据
		UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
		updateWrapper.set("is_send", "0");
		updateWrapper.set("is_msg", "0");
		updateWrapper.eq("app_id", "wx4a8ce56c55215ee0");
		sysUserService.update(updateWrapper);
		
		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("status", "0");
		queryWrapper.eq("is_tip", "0");
		queryWrapper.eq("is_send", "0");
		queryWrapper.eq("app_id", "wx4a8ce56c55215ee0");
		queryWrapper.orderByAsc("login_date");
		IPage<SysUser> page = new Page<>(1, 20);
		IPage<SysUser> res = sysUserService.page(page, queryWrapper);
		for(int c=1;c<=res.getPages();c++){
			page.setCurrent(c);
			queryWrapper.or().eq("is_msg", "1");
			IPage<SysUser> data = sysUserService.page(page, queryWrapper);
			List<SysUser> userList = data.getRecords();
			for(SysUser user : userList){
				if(user.getLoginDate() != null){
					if("0".equals(user.getIsSend())){
						long day = DateUtil.betweenDay(user.getLoginDate(), new Date(), true);
						if(day > 0){
							// 超过一天没上线发送通知
							WxMaMsgForm msgForm = new WxMaMsgForm();
							msgForm.setAppId(user.getAppId());
							msgForm.setOpenId(user.getOpenId());
							Map<String,String> dataMap = new HashMap<>();
							dataMap.put("phrase1", "新动态通知");
							dataMap.put("date2", DateUtil.now());
							dataMap.put("name3", "匿名用户");
							dataMap.put("thing4", "又有新动态更新了，快去看看吧");
							msgForm.setDataMap(dataMap);
							msgForm.setPage("pages/index");
							boolean flag = wxMsgService.sendSubscribeMsg(msgForm);
							if(flag){
								user.setIsMsg("1"); // 消息补偿
								user.setIsSend("1"); // 通知过
								sysUserService.updateById(user);
							}else{
								// 小程序没通知成功，继续用公众号通知
								WxMpMsgForm mpForm = new WxMpMsgForm();
								mpForm.setWxId(user.getWxId());
								mpForm.setAppId(user.getAppId());
								mpForm.setOpenId(user.getWxOpenId());
								Map<String,String> mpMap = new HashMap<>();
								mpMap.put("first", "新动态通知");
								mpMap.put("keyword1", "匿名用户");
								mpMap.put("keyword2", DateUtil.now());
								mpMap.put("keyword3", "又有新动态更新了，快去看看吧");
								mpMap.put("remark", "本条信息是站内信通知，祝您早日脱单！点击查看详情");
								mpForm.setDataMap(mpMap);
								mpForm.setPage("pages/index");
								boolean mpFlag = wxMsgService.sendTemplateMsg(mpForm);
								if(!mpFlag){
									user.setIsTip("1"); // 不能接收到消息
								}
								user.setIsMsg("1"); // 消息补偿
								user.setIsSend("1"); // 通知过
								sysUserService.updateById(user);
							}
						}
					}
				}
			}
		}
	}
	
	//签到提醒
	//@Scheduled(cron = "0 4 12 * * ?")
	public void cronTest33(){
		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
		SysUser entity = new SysUser();
		entity.setAppId("wx4a8ce56c55215ee0");
		entity.setStatus("0");
		queryWrapper.setEntity(entity);
		queryWrapper.between("create_time", "2024-05-14 00:01:44", "2024-05-15 00:01:44");
		queryWrapper.orderByAsc("create_time");
		List<SysUser> userList = sysUserService.list(queryWrapper);
		System.out.println("总共用户："+userList.size()+"人");
		
		/*Integer sucess = 0;
		for(int i=0;i<userList.size();i++){
			SysUser user = userList.get(i);
			System.out.println("userId:"+user.getUserId());
			
			WxMaMsgForm msgForm = new WxMaMsgForm();
			msgForm.setAppId(user.getAppId());
			msgForm.setOpenId(user.getOpenId());
			Map<String,String> dataMap = new HashMap<>();
			dataMap.put("phrase1", "签到提醒");
			dataMap.put("date2", DateUtil.now());
			dataMap.put("name3", "系统消息");
			dataMap.put("thing4", "每日签到提醒");
			msgForm.setDataMap(dataMap);
			msgForm.setPage("pages/index?index=4");
			boolean isSend = wxMsgService.sendSubscribeMsg(msgForm);
			if(isSend){
				sucess++;
			}
		}
		
		System.out.println("发送成功："+sucess+"个");*/
	}
	
	//@Scheduled(cron = "0 */30 * * * ?")
	public void cronTestff(){
		Integer start = redisCache.getCacheObject("start");
		if(start == null){
			start = 0;
			redisCache.setCacheObject("start", start);
		}
		Integer end = redisCache.getCacheObject("end");
		if(end == null){
			end = 250;
			redisCache.setCacheObject("end", end);
		}
		
		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
		SysUser entity = new SysUser();
		entity.setWxId("gh_bbe02df1d5be");
		entity.setAppId("wx2b5596fc2c4454d4");
		queryWrapper.setEntity(entity);
		queryWrapper.isNotNull("wx_open_id");
		queryWrapper.orderByAsc("user_id");
		List<SysUser> userList = sysUserService.list(queryWrapper);
		if(start > userList.size()){
			start = 0;
			end = 250;
			redisCache.setCacheObject("start", start);
			redisCache.setCacheObject("end", end);
		}
		if(end > userList.size()){
			end = userList.size();
			redisCache.setCacheObject("end", end);
		}
		for(int i=start;i<end;i++){
			SysUser user = userList.get(i);
			if("1".equals(user.getIsBlack()) || "1".equals(user.getStatus())){
				continue;
			}
			SysUser ru = sysUserService.getRandomUserByUser(user);
			String pagePath = "pages/index";
			if(ru != null){
				pagePath = pagePath + "?userId="+ru.getUserId();
			}
			WxMpMsgForm msgForm = new WxMpMsgForm();
			msgForm.setWxId(user.getWxId());
			msgForm.setAppId(user.getAppId());
			msgForm.setOpenId(user.getWxOpenId());
			Map<String,String> dataMap = new HashMap<>();
			dataMap.put("first", "您好，您收到一个好友申请");
			dataMap.put("keyword1", "月老牵线");
			dataMap.put("keyword2", DateUtil.now());
			dataMap.put("keyword3", "为你分配了一个对象，点击进入");
			dataMap.put("remark", "本条信息是站内信通知，祝您早日脱单！点击查看详情");
			msgForm.setDataMap(dataMap);
			msgForm.setPage(pagePath);
			wxMsgService.sendTemplateMsg(msgForm);
			
			PayCoinUserForm payCoinUserForm = new PayCoinUserForm();
	    	payCoinUserForm.setCoinNum(200);
	    	payCoinUserForm.setCoinType("0");
	    	payCoinUserForm.setRemark("活动奖励");
	    	payCoinUserForm.setUserId(user.getUserId());
	    	
	    	PayCoinUser coinUser = new PayCoinUser();
			coinUser.setUserId(payCoinUserForm.getUserId());
			coinUser.setCoinNum(payCoinUserForm.getCoinNum());
			coinUser.setCoinType(payCoinUserForm.getCoinType());
			coinUser.setRemark(payCoinUserForm.getRemark());
			payCoinUserService.save(coinUser);
		}
		start = end;
		end = end + 250;
		redisCache.setCacheObject("start", start);
		redisCache.setCacheObject("end", end);
	}
	
	//@Scheduled(cron = "0 */30 * * * ?")
	public void cronTest6(){
		WxMaForm wxMaForm = new WxMaForm();
		QueryWrapper<PayCoinCash> qw = new QueryWrapper<>();
		PayCoinCash entity = new PayCoinCash();
		entity.setStatus("0");
		qw.setEntity(entity);
		List<PayCoinCash> cashList = payCoinCashService.list(qw);
		for(PayCoinCash cash : cashList){
			if(StringUtils.isNotBlank(cash.getAppId())){
				wxMaForm.setAppId(cash.getAppId());
				wxMaForm.setTradeNo(cash.getTradeNo());
				WxPayRedpackQueryResult result = wxMaPayService.queryRedpack(wxMaForm);
				if("RECEIVED".equals(result.getStatus())){
					// 已领取
					cash.setStatus("1");
					payCoinCashService.updateById(cash);
				}else if("FAILED".equals(result.getStatus())){
					// 发放失败
					payCoinCashService.removeById(cash.getId());
				}
			}
		}
	}
	
	//@Scheduled(cron = "0 0 9 * * ?")
	public void cronTestffd(){
		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
		SysUser entity = new SysUser();
		entity.setAppId("wx4a8ce56c55215ee0");
		entity.setStatus("0");
		//entity.setPhonenumber("15058899783");
		queryWrapper.setEntity(entity);
		queryWrapper.isNotNull("wx_open_id");
		queryWrapper.gt("user_id", 11534534);
		queryWrapper.orderByAsc("user_id");
		List<SysUser> userList = sysUserService.list(queryWrapper);
		int count = 0;
		for(SysUser user : userList){
			String pagePath = "pages/index?index=4";
			WxMpMsgForm msgForm = new WxMpMsgForm();
			msgForm.setWxId(user.getWxId());
			msgForm.setAppId(user.getAppId());
			msgForm.setOpenId(user.getWxOpenId());
			Map<String,String> dataMap = new HashMap<>();
			dataMap.put("thing6", user.getNickName());
			dataMap.put("time10", DateUtil.now());
			msgForm.setDataMap(dataMap);
			msgForm.setPage(pagePath);
			boolean flag = wxMsgService.sendTemplateMsg(msgForm, "9Y_wuJJvnR744-K4PGdLwCuqQthRUB9p2Pph7yqyka8");
			if(flag){
				count++;
				System.out.println("发送成功："+count);
			}
		}
		
	}
	
	//@Scheduled(cron = "0 53 11 * * ?")
	public void cronTest8(){
		redisCache.deleteKeyPre("login_tokens");
		System.out.println("清除登陆信息");
	}
	
	// 公众号菜单
	//@Scheduled(cron = "0 42 22 * * ?")
	public void menu(){
		System.out.println("开始执行");
		wxMpUserService.menuCreate("wxe7594bc825b3c35d");
	}
	
	//@Scheduled(cron = "0 */30 * * * ?")
	public void cronTest8888() {
		QueryWrapper<WqDynamic> queryWrapper = new QueryWrapper<>();
		WqDynamic entity = new WqDynamic();
		entity.setStatus("1");
		entity.setDelFlag("0");
		queryWrapper.setEntity(entity);
		List<WqDynamic> dyList = wqDynamicService.list(queryWrapper);
		for(WqDynamic dynamic : dyList){
			dynamic.setDelFlag("1");
			wqDynamicService.updateById(dynamic);
		}
	}
	
	
	//@Scheduled(cron = "0 0 14 * * ?")
	public void cronTest89999() {
		System.out.println("开始执行");
		List<SysUser> userList = sysUserService.getVipUserList("wx4a8ce56c55215ee0");
		int count = 0;
		int send = 0;
		int fail = 0;
		for(SysUser user : userList){
			QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
			SysUser entity = new SysUser();
			entity.setAppId("wx4a8ce56c55215ee0");
			entity.setUnionId(user.getUnionId());
			queryWrapper.setEntity(entity);
			SysUser loginUser = sysUserService.getOne(queryWrapper);
			if(loginUser == null){
				WxMpMsgForm msgForm = new WxMpMsgForm();
        		msgForm.setWxId(user.getWxId());
        		msgForm.setAppId("wx2708b75e03e8440a");
        		msgForm.setOpenId(user.getWxOpenId());
        		Map<String,String> dataMap = new HashMap<>();
        		dataMap.put("first", "您好，您有一天会员待领取");
        		dataMap.put("keyword1", "会员福利");
        		dataMap.put("keyword2", DateUtil.now());
        		dataMap.put("keyword3", "您好，您有一天会员待领取");
        		dataMap.put("remark", "本条信息是站内信通知，祝您早日脱单！点击查看详情");
        		msgForm.setDataMap(dataMap);
        		msgForm.setPage("pages/index?shareUserId=11581860");
        		boolean flag = wxMsgService.sendTemplateMsg(msgForm);
        		if(flag){
        			send++;
        		}else{
        			fail++;
        		}
			}else{
				count++;
			}
		}
		
		System.out.println("总共："+userList.size()+"个用户");
		System.out.println("已领取："+count+"人");
		System.out.println("发送成功："+send+"人");
		System.out.println("发送失败："+fail+"人");
	}
	
}
